<template>
  <div>
    <van-nav-bar title="职位类型" left-arrow @click-left="leftBack" />
    <div class="position-type">
      <div class="position-type-left">
        {{state.typeKey}}
        <h5
          v-for="(item, index) in store.positionList"
          :class="state.typeKey == index ? 'active' : ''"
          :key="index"
          @click="setTypeKey(index)"
        >
          {{ item.name }}
        </h5>
      </div>
      <div class="position-type-right">
        <span
          @click="closePositionType(item.name)"
          v-for="(item, index) in store.positionList[state.typeKey]?.children"
          :key="index"
          >{{ item.name }}</span
        >
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { positionTypeList } from "@/api/task.ts";
import { showToast } from "vant";
import { reactive, inject } from "vue";
import { taskStore } from "../../../store/task.ts";

// 声明inject
const { closePositionType } = inject("popup");

const leftBack = () => closePositionType();
const state = reactive({
  typeKey: 0,
});
// 引用pinia的store
const store = taskStore();
const getPositionTypeList = async () => {
  const res = await positionTypeList();
  if (res) {
    store.setPositionList(res);
  } else {
    showToast(res.msg);
  }
};
if (store.positionList.length <= 0) {
  getPositionTypeList();
}

const setTypeKey = (key:number) => {
  console.log(key,'--key')
  state.typeKey = key;
};
</script>

<style lang="scss" scoped>
.position-type {
  display: flex;
  &-left {
    width: 4.77rem;
    height: calc(100vh - 46px);
    background: #f6f6f6;
    h5 {
      width: 4.77rem;
      height: 2.4rem;
      background: #f6f6f6;
      border-bottom: 1px solid #fff;
      line-height: 2.4rem;
      font-size: 0.75rem;
      font-weight: 300;
      color: #333;
      text-align: center;
      &:active {
        background-color: #efefef;
      }
    }
  }
  &-right {
    padding: 0.8rem 0 0 0.8rem;
    span {
      width: 6.61rem;
      height: 2.03rem;
      line-height: 2.03rem;
      background: #fff;
      border: 1px solid #e9e9e9;
      border-radius: 0.11rem;
      font-size: 0.75rem;
      font-weight: 300;
      color: #666;
      text-align: center;
      margin-right: 0.59rem;
      margin-bottom: 0.64rem;
      display: inline-block;
      &:nth-child(2n) {
        margin-right: 0;
      }
    }
  }
}
</style>
